package gamagora.game.entity
{
	import gamagora.game.entity.OSEntity;
	import gamagora.game.resources.OSImages;
	import gamagora.game.resources.OSSounds;
	import gamagora.game.world.maps.OSMap;
	
	import org.flixel.FlxG;
	import org.flixel.FlxSprite;
	
	public class OSBird extends OSEntity
	{
		public static const IDLE:String = "idle";
		public static const START_FLY:String = "start";
		public static const STOP_FLY:String = "stop";
		public static const FLY:String = "fly";
		
		private static const SPEED_X:Number = 30.0;
		private static const SPEED_Y:Number = 30.0;
		private static const PLAYER_DIST_BEFORE_FLY:Number = 200.0;
		private static const Y_DIST_BEFORE_KILL:Number = -40.0;
		private static const WAIT_TIME:Number = 3.0;
		
		private var _background:Boolean;
		private var _dirX:Number;
		private var _flyingAtStart:Boolean;
		private var _timeBeforeUp:Number;
		private var _counter:Number;
		private var _animOnly:Boolean;
		private var _speedX:Number;
		private var _speedY:Number;
		private var _start:Boolean;
		private var _goDown:Boolean;
		private var _waitTimer:Number;
		
		public function OSBird(X:Number=0, Y:Number=0, background:Boolean = false, flyingAtStart:Boolean = false, timeBeforeUp:Number = 20, animOnly:Boolean = false)
		{
			super(ANIMATION, X, Y);
			_background = background;
			_flyingAtStart = flyingAtStart;
			_timeBeforeUp = timeBeforeUp;
			_animOnly = animOnly;
			_counter = 0;
			_start = true;
			_waitTimer = 0;
			
			if(background)
			{
				loadGraphic(OSImages.BIRD2, true, true, 36, 36);
				offset.x = 18;
				offset.y = 26;
				_speedX = SPEED_X;
				_speedY = SPEED_Y;
			}
			else 
			{
				loadGraphic(OSImages.BIRD, true, true, 72, 72);
				offset.x = 20;
				offset.y = 12;
				_speedX = SPEED_X * 2;
				_speedY = SPEED_Y * 2;
			}
			
			addAnimation(IDLE, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0], uint(Math.random() * 4 + 4));
			addAnimation(START_FLY, [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25], 20, false);
			addAnimation(STOP_FLY, [25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10], 20, false);
			addAnimation(FLY, [26, 27, 28, 29, 30, 31, 32, 33, 34], 20);
			
			if(!_flyingAtStart)
			{
				var angle:Number = (Math.random() * 60);
				_dirX = Math.cos(Math.PI * angle / 180);
				_dirX *= (uint(Math.random() * 2) == 0) ? -1 : 1;
				_facing = _dirX > 0 ? LEFT : RIGHT;
				play(IDLE);
			}
			else 
			{
				_dirX = _facing == LEFT ? 1 : -1;
				play(FLY);
			}
		}
		
		public override function update():void
		{
			if(_flyingAtStart)
			{
				if(!_animOnly)
				{
					x += FlxG.elapsed * _dirX * _speedX;
					
					if(_counter >= _timeBeforeUp)
					{
						y -= FlxG.elapsed * _speedY;
					}
					else 
					{
						_counter += FlxG.elapsed;
					}
					
					if(y < Y_DIST_BEFORE_KILL)
					{
						kill();
					}
				}
				
				if(!_animOnly && y < Y_DIST_BEFORE_KILL)
				{
					kill();
				}
			}
			else 
			{
				switch(_curAnim.name)
				{
					case IDLE:
						if(!_animOnly && Math.abs(x + (_background ? _scroll : 0) - _world.getPlayerX()) < PLAYER_DIST_BEFORE_FLY)
						{
							play(START_FLY);
						}
						break;
					case START_FLY:
						if(finished)
						{
							play(FLY);
						}
						break;
					case STOP_FLY:
						if(finished)
						{
							play(IDLE);
						}
						break;
					case FLY:
						if(_goDown)
						{
							x -= FlxG.elapsed * _dirX * _speedX;
							y += FlxG.elapsed * _speedY;
						}
						else
						{
							x += FlxG.elapsed * _dirX * _speedX;
							y -= FlxG.elapsed * _speedY;
						}
						break;
				}

				if(!_goDown)
				{
					if(!_animOnly && y < Y_DIST_BEFORE_KILL)
					{
						_waitTimer += FlxG.elapsed;
						if(_waitTimer >= WAIT_TIME)
						{
							//Go back
							_goDown = true;
							_facing = _facing == LEFT ? RIGHT : LEFT;
							_waitTimer = 0;
						}
					}
				}
				else
				{
					if(y >= _startY)
					{
						y = _startY;
						x = _startX;
						_dirX *= -1;
						_goDown = false;
						_start = true;
						play(STOP_FLY);
					}
				}
			}
			
			if(_curAnim.name == FLY && _start)
			{
				if(Math.random() > 0.9)
				{
					OSSounds.playFlightBird(0.2);
					_start = false;
				}
			}
			
			super.update();
		}
		
		public override function resetEntity():void
		{
			super.resetEntity();
			_counter = 0;
			if(!_flyingAtStart)
			{
				play(IDLE);
			}
			else 
			{
				play(FLY);
			}
			_start = true;
			_goDown = false;
			_waitTimer = 0;
		}

		public function get animOnly():Boolean
		{
			return _animOnly;
		}

		public function set animOnly(value:Boolean):void
		{
			_animOnly = value;
		}

		public function get speedX():Number
		{
			return _speedX;
		}

		public function set speedX(value:Number):void
		{
			_speedX = value;
		}

		public function get speedY():Number
		{
			return _speedY;
		}

		public function set speedY(value:Number):void
		{
			_speedY = value;
		}
	}
}
